package br.usp.icmc.dilvan.aimTemplateViewer.client.impl;

import br.usp.icmc.dilvan.aimTemplateViewer.client.ComponentsAIM;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aim.ImageAnnotationAIM;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aimTemplate.ComponentAIMTemplate;
import br.usp.icmc.dilvan.aimTemplateViewer.client.aimTemplate.TemplateAIMTemplate;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ComponentsAIMImpl extends Composite implements ComponentsAIM{

	private static ComponentsAIMUiBinder uiBinder = GWT
			.create(ComponentsAIMUiBinder.class);
	interface ComponentsAIMUiBinder extends UiBinder<Widget, ComponentsAIMImpl> {
	}

	@UiField VerticalPanel componentsPanel;
	@UiField Button buttonSave;
	private final ImageAnnotationAIM result = new ImageAnnotationAIM();
	Presenter presenter;
	
	public ComponentsAIMImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public ComponentsAIMImpl(TemplateAIMTemplate template) {
		initWidget(uiBinder.createAndBindUi(this));
		createForm(template);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	public void createForm(TemplateAIMTemplate template) {
		componentsPanel.clear();
		result.setImageReferenceUID("ImageReferenceUID");
		result.setRoiID("");
		result.setTemplateContainerID("");
		result.setTemplateID("");
		result.setUserID("");
		for (ComponentAIMTemplate componente: template.getComponents()) {
			// set up components from the Template
			ComponentAIMImpl comAim = new ComponentAIMImpl(componente);
			componentsPanel.add(comAim);
			// get the components response from the UI
//			result.getResponseTemplate().add(comAim.getResult());
			
		}
	}
	
	public void createForm(String imageReferenceUID, String roiID, String templateContainerID, String templateID, String userID, TemplateAIMTemplate template) {
		componentsPanel.clear();
		result.setImageReferenceUID(imageReferenceUID);
		result.setRoiID(roiID);
		result.setTemplateContainerID(templateContainerID);
		result.setTemplateID(templateID);
		result.setUserID(userID);
		for (ComponentAIMTemplate componente: template.getComponents()) {
			// set up components from the Template
			ComponentAIMImpl comAim = new ComponentAIMImpl(componente);
			componentsPanel.add(comAim);
			// get the components response from the UI
//			result.getResponseTemplate().add(comAim.getResult());
			
		}
	}

	@UiHandler("buttonSave")
	void onButtonSaveClick(ClickEvent event) {
		//send to presenter the result object from the form
		presenter.sendResponseForm(result);
	}
	
}
